package cn.zswltech.gruul.web.api.controller;

import cn.zswltech.gruul.api.MenuApi;
import cn.zswltech.gruul.biz.service.FunctionService;
import cn.zswltech.gruul.biz.service.MenuService;
import cn.zswltech.gruul.common.dto.*;
import cn.zswltech.gruul.common.result.Response;
import cn.zswltech.gruul.common.util.StringUtil;
import cn.zswltech.gruul.common.entity.FunctionDO;
import cn.zswltech.gruul.common.entity.MenuDO;
import cn.zswltech.gruul.web.api.controller.common.BaseController;
import cn.zswltech.gruul.web.api.util.AccountUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class MenuController extends BaseController implements MenuApi {


    @Resource(name = "menuService")
    private MenuService menuService;

    @Resource(name = "functionService")
    private FunctionService functionService;

    /**
     * 根据menuCode查询功能列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public Response<List<FunctionDO>> queryByMenuId() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return functionService.queryByMenuId(Long.valueOf(request.getParameter("menuId")));
    }

    /**
     * 删除功能
     *
     * @return
     * @throws Exception
     */
    @Override
    public Response deleteFunction(Long id) {
        return functionService.deleteFunction(id);
    }

    /**
     * 更新功能
     *
     * @return
     * @throws Exception
     */
    @Override
    public Response updateFunction() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        FunctionDO functionDO = genFunctionDO(request);
        functionDO.setId(Long.valueOf(request.getParameter("id")));
        return functionService.updateFunction(functionDO);
    }

    /**
     * 创建功能
     * @return
     * @throws Exception
     */
    @Override
    public Response createFunction() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        FunctionDO functionDO = genFunctionDO(request);
        Date now = new Date();
        functionDO.setGmtCreate(now);
        functionDO.setGmtModified(now);
        return functionService.createFunction(functionDO);
    }

    /**
     * 获取全部菜单
     *
     * @returnx
     * @throws Exception
     */
    @Override
    public Response<List<MenuDO>> getMenuList() {
        return menuService.getMenuList();
    }

    /**
     * 创建菜单
     * @return
     * @throws Exception
     */
    @Override
    public Response createMenu() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        MenuDO menuDO = genMenuDO(request);
        Date now = new Date();
        menuDO.setGmtCreate(now);
        menuDO.setGmtModified(now);
        menuDO.setSortNo(0);
        return menuService.createMenu(menuDO);
    }

    /**
     * 更新菜单
     *
     * @return
     * @throws Exception
     */
    @Override
    public Response updateMenu() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        MenuDO menuDO = genMenuDO(request);
        menuDO.setId(Long.valueOf(request.getParameter("id")));
        return menuService.updateMenu(menuDO);
    }

    /**
     * 删除菜单

     * @return
     * @throws Exception
     */
    @Override
    public Response deleteMenu(@Param("id") Long id) {
        return menuService.deleteMenu(id);
    }

    @Override
    public Response<List<MenuInfo>> underGroupMenuList(MenuListREQ req) {
        List<MenuInfo> result = null;
        try {
            result = menuService.underGroupMenuList(req);
        } catch (Exception e) {
            log.error("查询菜单分组：{}下菜单失败", req.getCustomTreeId(),e);
            return Response.error(500,"菜单分组下菜单查询失败");
        }
        return Response.success(result);
    }

    @Override
    public Response underGroupMenuAdd(UnderGroupMenuAddREQ req) {
        try {
            menuService.underGroupMenuAdd(req);
        } catch (Exception e) {
            log.error("新增菜单失败 req:{}", req,e);
            return Response.error(500, "新增菜单失败");
        }
        return Response.success();
    }

    @Override
    public Response underGroupMenuUpdate(UnderGroupMenuUpdateREQ req) {
        try {
            menuService.underGroupMenuUpdate(req);
        } catch (Exception e) {
            log.error("菜单编辑失败 req:{}", req,e);
            return Response.error(500, "菜单编辑失败");
        }
        return Response.success();
    }

    @Override
    public Response underGroupMenuDel(UnderGroupMenuDelREQ req) {
        try {
            menuService.underGroupMenuDel(req);
        } catch (Exception e) {
            log.error("菜单删除失败 req:{}", req,e);
            return Response.error(500, "菜单删除失败");
        }
        return Response.success();
    }

    @Override
    public Response<List<GruulMenu>> gruulMenuList() {
        String account = AccountUtil.getAccount();
        try {
            List<GruulMenu> list = menuService.gruulMenuList(account);
            return Response.success(list);
        } catch (Exception e) {
            log.error("用户：{}，门户菜单列表获取失败",account,e);
            return Response.error(500, "获取门户菜单列表失败");
        }
    }

    private MenuDO genMenuDO(HttpServletRequest request) {
        MenuDO menuDO = new MenuDO();
        menuDO.setCode(request.getParameter("code"));
        menuDO.setIcon(request.getParameter("icon"));
        menuDO.setName(request.getParameter("name"));
        menuDO.setEnName(request.getParameter("enName"));
        menuDO.setPath(request.getParameter("path"));
        menuDO.setTarget(request.getParameter("target"));
        menuDO.setLevel(StringUtil.str2Integer(request.getParameter("level")));
        menuDO.setParentId(Long.valueOf(request.getParameter("parentId")));
        return menuDO;
    }

    private FunctionDO genFunctionDO(HttpServletRequest request) {
        FunctionDO functionDO = new FunctionDO();
        functionDO.setCode(request.getParameter("code"));
        functionDO.setName(request.getParameter("name"));
        functionDO.setMethod(request.getParameter("method"));
        functionDO.setPath(request.getParameter("path"));
        functionDO.setEnName(request.getParameter("enName"));
        functionDO.setMenuId(Long.valueOf(request.getParameter("menuId")));
        return functionDO;
    }
}
